package com.tt.service;

import com.tt.common.Const.TtConstans;
import com.tt.common.exception.TtException;
import com.tt.mapper.RegionMapper;
import com.tt.pojo.system.Region;
import com.tt.pojo.system.RegionTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 青青
 */
@Service
public class RegionServiceImpl implements IRegionService
{
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public int addRegion(Region region)
    {
        Integer pId = region.getPId();
        Region r = Region.builder().id(pId).build();

        Region getRegion = getRegion(r);
        if (!StringUtils.isEmpty(getRegion))
        {
            region.setParentCode(getRegion.getCode());
        }
        return regionMapper.addRegion(region);
    }

    @Override
    public Region getRegion(Region region)
    {
        return regionMapper.getRegion(region);
    }

    @Override
    public List<Region> getRegionByPid(Integer pid)
    {
        List<Region> regionList = regionMapper.getRegionList(pid);
        return regionList;
    }

    @Override
    public int addBatchRegion(List<Region> region)
    {
        int i = regionMapper.addBatchRegion(region);
        return i;
    }

    @Override
    public int updateRegion(Region region) throws TtException
    {
        int i = regionMapper.updateRegion(region);
        if (i <= 0)
        {
            throw new TtException("更新发现问题，快联系程序员");
        }
        return i;
    }

    @Override
    public int delBatchRegion(List<Integer> ids)
    {
        int i = regionMapper.delBatchRegion(ids);
        if (i <= 0)
        {
            throw new TtException("删除不成功，快联系程序员");
        }
        return i;
    }

    @Override
    public int delRegion(Integer id) throws TtException
    {
        int i = 0 ;
        Region region = regionMapper.getRegion(Region.builder().id(id).build());
        if (StringUtils.isEmpty(region))
        {
            throw new TtException("找不到该节点数据 ，请刷新");
        }
        String charin = region.getCharin();
        List<Integer> byCharin = regionMapper.getRegionListByCharin(charin+TtConstans.Connect_x);

        //如果有孩子
        if (!CollectionUtils.isEmpty(byCharin))
        {
             i = regionMapper.delRegionByIds(byCharin );
        }

       // 删除自己
        List<Integer> add = new ArrayList();
        add.add(region.getId());
        i += regionMapper.delBatchRegion(add);
        return i;
    }

    @Override
    public List<RegionTreeVo> getRegionList(Integer pid)
    {
        List<RegionTreeVo> list = new ArrayList<>();
        List<Region> regionList = regionMapper.getRegionList(pid);
        for (Region region: regionList)
        {
            RegionTreeVo rev = new RegionTreeVo();
            rev.setId(region.getId());
            rev.setName(region.getName());
            rev.setParentCode(region.getParentCode());
            rev.setLevel(region.getLevel());
            rev.setCharin(region.getCharin());
            rev.setOpen(false);
            isParent(region.getId(),rev);
            rev.setPId(region.getPId());
            list.add(rev);
        }
        return list;
    }

    /**
     * 判断是不受parent
     * @param id
     * @param rtv
     */
    private void isParent(Integer id,RegionTreeVo rtv)
    {
        //查询这个id下的孩子
        List<Region> regions = regionMapper.getRegionById(id);
        if (regions.size() > 0)
        {
            rtv.setIsParent(true);
        }
        else{
            rtv.setIsParent(false);
        }
    }





}
